﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace Zhuang.Utility.Common.Compression
{
//// <summary>
    /// 压缩/解压缩辅助类
    /// </summary>
    public sealed class ZipHelper
    {
        private ZipHelper()
        {
        }

        /**//// <summary>
        /// 压缩文件操作
        /// </summary>
        /// <param name="fileToZip">待压缩文件名</param>
        /// <param name="zipedFile">压缩后的文件名</param>
        /// <param name="compressionLevel">0~9,表示压缩的程度，9表示最高压缩</param>
        /// <param name="blockSize">缓冲块大小</param>
        public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
        {
            if (! File.Exists(fileToZip))
            {
                throw new FileNotFoundException("文件 " + fileToZip + " 没有找到，取消压缩。");
            }

            using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
            {
                FileStream fileStream = File.Create(zipedFile);
                using (ZipOutputStream zipStream = new ZipOutputStream(fileStream))
                {
                    ZipEntry zipEntry = new ZipEntry(fileToZip);
                    zipStream.PutNextEntry(zipEntry);
                    zipStream.SetLevel(compressionLevel);

                    byte[] buffer = new byte[blockSize];
                    Int32 size = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, size);

                    try
                    {
                        while (size < streamToZip.Length)
                        {
                            int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, sizeRead);
                            size += sizeRead;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    zipStream.Finish();
                }
            }
        }

        /**//// <summary>
        /// 打开sourceZipPath的压缩文件，解压到destPath目录中
        /// </summary>
        /// <param name="sourceZipPath">待解压的文件路径</param>
        /// <param name="destPath">解压后的文件路径</param>
        public static void UnZipFile(string sourceZipPath, string destPath)
        {
            if (!destPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                destPath = destPath + Path.DirectorySeparatorChar;
            }

            using (ZipInputStream zipInputStream = new ZipInputStream(System.IO.File.OpenRead(sourceZipPath)))
            {
                ZipEntry theEntry;
                while ((theEntry = zipInputStream.GetNextEntry()) != null)
                {
                    string fileName = destPath + Path.GetDirectoryName(theEntry.Name) +
                        Path.DirectorySeparatorChar + Path.GetFileName(theEntry.Name);

                    //create directory for file (if necessary)
                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                    if (!theEntry.IsDirectory)
                    {
                        using (FileStream streamWriter = System.IO.File.Create(fileName))
                        {
                            int size = 2048;
                            byte[] data = new byte[size];
                            try
                            {
                                while (true)
                                {
                                    size = zipInputStream.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
        }
    }
    //压缩/解压缩辅助类ZipHelper测试代码：
    public class TestZipHelper
    {
        public static string Execute()
        {
            string result = string.Empty;
            result += "使用ZipHelper压缩/解压缩辅助类：" + "\r\n";

            try
            {
                ZipHelper.ZipFile("Web.Config", "Test.Zip", 6, 512);
                ZipHelper.UnZipFile("Test.Zip", "Test");

                result += "操作完成！\r\n \r\n";
            }
            catch (Exception ex)
            {
                result += string.Format("发生错误:{0}！\r\n \r\n", ex.Message);
            }
            return result;
        }
    }
}
